$(function () {
    var mlid = getParameter("mlid");
    //批量删除
    $("#delMore").click(function () {
        if (confirm("确认要移除吗？")){
            //1.添加判断状态
            var flag = false;
            $.each($(".itemSelect"), function (i,j) {
                if (j.checked) flag = true;
            });
        }
        if (flag){
            $.post("/music/musicList/remMoreMusic",$("#myForm").serialize(),{},"json");
            location.href = "musicInList.html?mlid="+mlid;
        }
    });
    load();
    $.get("/music/musicList/getOne",{mlid:mlid},function (data) {
        $("#musicListName").html("《"+data.musiclistname+"》");
    });
});
function load(currentPage) {
    var mlid = getParameter("mlid");
    $.get("/music/music/findMusicInListByPage",{currentPage:currentPage,mlid:mlid},function (data) {
        // console.log(data.list[0].musicList);
        console.log(data)
        if (data.totalCount===0){
            $("#errorMsg").html("歌单中还没有歌曲,<a href='addMusicToList.html?mlid="+mlid+"'>请去添加</a>")
        }
        if (data.list!=null){
            //获取tbody
            $tbody = $("#musicMsg");
            //拼接
            var trlist = '';
            for (var i in data.map){
                var tr ="<tr>" +
                    "       <td>" +
                    "           <label class='lyear-checkbox checkbox-primary'>" +
                    "           <input type='checkbox' name='mids' value='"+data.list[i].mid+"'" +
                    "                     id='' class='itemSelect'> <span></span>" +
                    "       </label>" +
                    "       </td>" +
                    "       <td>"+data.map[i][0]+"</td>" +
                    "       <td>"+data.map[i][1]+"</td>" +
                    "       <td>" +
                    "           <div class='btn-group'>" +
                    "           <a onclick='remMusicFromList("+data.list[i].mid+")' class='btn btn-xs btn-default' title='移除' data-toggle='tooltip'>" +
                    "               <i class='mdi mdi-minus'></i>" +
                    "           </a>" +
                    "           </div>" +
                    "       </td>" +
                    "</tr>"
                //
                trlist += tr;
            }
            //2.3 将liList添加到ul中
            $tbody.html(trlist);
            //4.处理页面 前5后4
            //4.1 开始页数 = 当前页数-5
            var start = data.currentPage-5;
            //4.2结束页数=当前页数+4
            var end = data.currentPage+4;
            //4.3判断前面不足5个的情况
            if (start<1){
                start=1;
                end=10;
            }
            //4.4判断后面不足4的情况
            if (end>data.totalPage){
                end = data.totalPage;
                start = end-9;
            }
            //4.5总页数不足10页
            if (data.totalPage<10){
                start = 1;
                end = data.totalPage;
            }
            //4.6拼接 ul -li
            $pageUl = $("#pageUl");
            //首页  cid = 3
            var pageList = '<li><a href="javaScript:load()">首页</a></li>';
            //上一页
            var prePage = data.currentPage - 1;
            if(prePage < 1){
                prePage = 1;
            }
            pageList += '<li>' +
                '<a href="javaScript:load('+prePage+')">上一页</a></li>';
            //拼接 页码 从start开始 end 结束
            for(var i = start ; i <= end; i++){
                if(i === data.currentPage){
                    pageList += '<li onclick="load('+i+')" class="active"><a href="javaScript:">'+i+'</a></li>';
                }else {
                    pageList += '<li onclick="load('+i+')" ><a href="javaScript:">'+i+'</a></li>';
                }
            }
            //下一页
            var nextPage = data.currentPage + 1;
            if (nextPage > data.totalPage){
                nextPage = data.totalPage;
            }
            pageList += '<li onclick="load('+nextPage+')" ><a href="javascript:;">下一页</a></li>';
            //末页
            pageList += '<li onclick="load('+data.totalPage+')" ><a href="javascript:;">末页</a></li>';
            //4.7 将pageList添加到ul中
            $pageUl.html(pageList);
        }
    },"json");
}
//移除歌单中的歌曲
function remMusicFromList(mid){
    var mlid = getParameter("mlid");
    $.post("/music/musicList/remMusic",{mid:mid},function (data) {
        alert("移除成功");
        location.href="musicInList.html?mlid="+mlid;
    });
}
